package com.nandi.deviceinstall.ui.activity

import android.Manifest
import android.content.Intent
import android.view.View
import com.nandi.deviceinstall.R
import com.nandi.deviceinstall.base.BaseMvpActivity
import com.nandi.deviceinstall.bean.LoginBean
import com.nandi.deviceinstall.common.Constant
import com.nandi.deviceinstall.contract.LoginContract
import com.nandi.deviceinstall.presenter.LoginPresenter
import com.nandi.deviceinstall.util.AesUtils
import com.nandi.deviceinstall.util.CustomToast
import com.nandi.deviceinstall.util.SharedTool
import com.nandi.deviceinstall.util.SxPermissionsUtils
import kotlinx.android.synthetic.main.activity_login.*

class LoginActivity : BaseMvpActivity<LoginPresenter>(),LoginContract.View {
    override val layoutId: Int
        get() = R.layout.activity_login

    override fun initView() {
        mPresenter = LoginPresenter()
        mPresenter.attachView(this)
        SxPermissionsUtils.with(this)
                .addPermission(Manifest.permission.READ_PHONE_STATE)
                .addPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                .addPermission(Manifest.permission.ACCESS_FINE_LOCATION)
                .addPermission(Manifest.permission.CAMERA)
                .addPermission(Manifest.permission.FACTORY_TEST)
                .addPermission(Manifest.permission.WRITE_SETTINGS)
                .addPermission(Manifest.permission.RECORD_AUDIO)
                .addPermission(Manifest.permission.INTERNET)
                .initPermission()
    }

    override fun showLoading() {
    }

    override fun hideLoading() {
    }

    override fun onError(msg: String, code: Int) {
        CustomToast.showToast(context,msg)
    }

    override fun onSuccess(bean: LoginBean) {
        SharedTool.put(context,Constant.TOKEN,"Bearer"+bean.access_token)
        startActivity(Intent(context, EquipmentBindingActivity::class.java).putExtra("action","add"))
       finish()
    }

    fun login(view: View) {
        val name: String = et_name.text.toString().trim()
        val pwd: String = et_pwd.text.toString().trim()
        mPresenter.login(name, AesUtils.encrypt(pwd,"thanks,pig4cloud"))
    }
}
